<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/5/16
 * Time: 16:10
 */

namespace Biz\tools;


class SensorsApiFilter
{
    const REL_AND='and';
    const REL_OR='and';
    private $relation;
    private $event;
    private $filters;
    public function __construct($event , $relation=self::REL_AND){
        $this->event = $event;
        $this->relation = $relation;
    }
    public function addEqualFilter($field , $value){
        $this->filters[] = array(
            'field' => $field,
            'func' => 'equal',
            'val' => strval($value)
        );
    }
    public function getFormated(){
        $a = array(
            'relation' => $this->relation,
            'conditions' => array()
        );

        foreach ($this->filters as $row) {
            $a['conditions'][] = array(
                'field' => 'event.'.$this->event.'.'.$row['field'],
                'function' => $row['func'],
                'params' => array($row['val']),
            );
        }
        return $a;
    }
    public function removeFilter(){
        $this->filters = array();
    }
}